package pri.jz.keeper.ui.register

import android.app.Application
import android.graphics.BitmapFactory
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import pri.jz.keeper.R
import pri.jz.keeper.repository.UserRepository
import pri.jz.keeper.entity.User
import pri.jz.keeper.repository.RegisterRepository
import pri.jz.keeper.ui.base.BaseViewModel
import pri.jz.keeper.ui.uistate.RegisterState
import pri.jz.keeper.ui.uistate.RegisterUiState

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS:
 */
class RegisterViewModel(private val application: Application) : BaseViewModel(application) {
    private val registerRepository by lazy {
        RegisterRepository()
    }
    private val _uiState = MutableStateFlow(RegisterUiState())
    val uiState = _uiState.asStateFlow()

    var numberText by mutableStateOf("")
    private set
    var passwordText by mutableStateOf("")
    private set
    var passwordConfirmText by mutableStateOf("")
    private set
    var codeText by mutableStateOf("")
    private set
    var codeBtnText by mutableStateOf("获取")
    private set

    private var code: String = "s"


    //----------------值改变监听
    fun onNumberTextChange(text: String) {
        numberText = text
        val b = numberText.length != 11
        numberState(b,if(b) "手机号格式不正确" else "手机号")
    }
    fun onPasswordTextChange(text: String) {
        passwordText = text
        val b = passwordText.length !in 6..20
        passwordState(b,if(b) "密码格式不正确" else "密码")
    }
    fun onPasswordConfirmTextChange(text: String) {
        passwordConfirmText = text
        val b = passwordConfirmText != passwordText
        passwordConfirmState(b,if(b) "两次密码不一致" else "确认密码")
    }
    fun onCodeTextChange(text: String) {
        codeText = text
        val b = codeText.length != 4
        codeState(b,if(b) "验证码格式不正确" else "验证码")
    }

    private fun numberState(isError: Boolean,label: String) {
        _uiState.update {
            it.copy(
                isNumberError = isError,
                numberLabel = label
            )
        }
    }
    private fun passwordState(isError: Boolean,label: String) {
        _uiState.update {
            it.copy(
                isPasswordError = isError,
                passwordLabel = label
            )
        }
    }
    private fun passwordConfirmState(isError: Boolean,label: String) {
        _uiState.update {
            it.copy(
                isPasswordConfirmError = isError,
                passwordConfirmLabel = label
            )
        }
    }
    private fun codeState(isError: Boolean,label: String) {
        _uiState.update {
            it.copy(
                isCodeError = isError,
                codeLabel = label
            )
        }
    }


    fun onCodeGain() {
        _uiState.update {
            it.copy(codeBtnState = false)
        }
        viewModelScope.launch(Dispatchers.IO) {
            //发送验证码
            code = "1111"
            for (i in 60 downTo 1) {
                codeBtnText = i.toString()
                delay(1000)
            }
            codeBtnText = "获取"
            _uiState.update {
                it.copy(codeBtnState = true)
            }
        }
    }

    fun onRegister() {
        uiState.value.apply {
            if (numberText.isBlank()) {
                numberState(true,"用户名不能为空")
            } else if (passwordText.isBlank()) {
                passwordState(true,"密码不能为空")
            } else if (passwordConfirmText.isBlank()) {
                passwordConfirmState(true,"密码不能为空")
            } else if (code.isBlank()) {
                codeState(true,"验证码不能为空")
            } else {
                requestLoadLaunch {
                    registerRepository.userRegister(
                        number = numberText,
                        password = passwordText,
                        onUserExist = {
                            showTip("手机号已注册")
                            numberState(true,"手机号已注册")
                        },
                        onRegisterFail = {
                             showTip("注册失败，请重试")
                        },
                        onRegisterSuccess = {
                            registerSuccess()
                        }
                    )
                }
            }
        }
    }

    private fun registerSuccess() {
        showTip("注册成功") {
            _uiState.update {
                it.copy(state = RegisterState.SUCCESS)
            }
        }
    }

    fun onFinish() {
        _uiState.update {
            it.copy(state = RegisterState.CANCEL)
        }
    }

}